Imports Microsoft.VisualBasic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports EntityLayer

Public Class CartaPresentacionDA

    Public Shared Function ObtenerNuevoCodigo() As String

        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myCommand As New SqlCommand("usp_ListNuevoIdCartaPresentacion", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure

        Dim para As SqlParameter

        para = New SqlParameter("@intCodCarta", SqlDbType.VarChar, 5)
        para.Direction = ParameterDirection.Output
        myCommand.Parameters.Add(para)

        myConnection.Open()

        myCommand.ExecuteNonQuery()

        myConnection.Close()

        Return myCommand.Parameters("@intCodCarta").Value

    End Function

    Public Shared Function Agregar(ByVal CartaEN As CartaPresentacionE) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("usp_AddCartaPresentacion", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@strNroCarta", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.NumeroCarta
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@dtFechaEmision", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.FechaEmision
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strDestinatario", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.Destinatario
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strAprobacion", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.Aprobacion
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strProveedor", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.Codigo
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strDescripcion", SqlDbType.VarChar, 200)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.Descripcion
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function ListarCartasPresentacion() As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("usp_ListCartaPresentacion", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        myDataadapter.Fill(ds, "Cartas")

        Return ds

    End Function

    Public Shared Function ListarCartaPresentacionxCodigo(ByVal strIdCarta As String) As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("usp_ListCartaPresentacionByCodigo", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        Dim para As SqlParameter

        para = New SqlParameter("@strIdCarta", SqlDbType.VarChar, 5)
        para.Direction = ParameterDirection.Input
        para.Value = strIdCarta
        myDataadapter.SelectCommand.Parameters.Add(para)

        myDataadapter.Fill(ds, "Cartas")

        Return ds

    End Function

    Public Shared Function Eliminar(ByVal strIdCarta As String) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("usp_DelCartaPresentacion", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@strIdCarta", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = strIdCarta
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function Actualizar(ByVal CartaEN As CartaPresentacionE) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("usp_UpdCartaPresentacion", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@strNroCarta", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.NumeroCarta
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@dtFechaEmision", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.FechaEmision
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strDestinatario", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.Destinatario
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strAprobacion", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.Aprobacion
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strProveedor", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.Codigo
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strDescripcion", SqlDbType.VarChar, 200)
            para.Direction = ParameterDirection.Input
            para.Value = CartaEN.Descripcion
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

End Class
